package com.lixin;

import com.lixin.bean.Order;
import com.lixin.bean.User;
import com.lixin.config.MyConfig;
import com.lixin.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApplication {

    public static void main(String[] args) {

       //testXml();
        testConfig();

    }

    // 基于xml配置文件的形式
    public static void testXml() {
        // 获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserServiceImpl userService = context.getBean("userService", UserServiceImpl.class);
        userService.getUserList();
    }

    // 基于@Configuration注解的形式
    public static void testConfig() {
        // 获取Spring的上下文对象
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        String[] names = context.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }

        User user1 = context.getBean("getUser", User.class);
        System.out.println(user1.getClass().getClassLoader());
        User user2 = context.getBean("getUser", User.class);
        System.out.println(user1);
        System.out.println(user2);

        Order order1 = context.getBean("getOrder", Order.class);
        Order order2 = context.getBean("getOrder", Order.class);
        System.out.println(order1);
        System.out.println(order2);
    }
}
